package com.backedserver.controller.judicialcheck;

import com.common.base.BindCreateBy;
import com.common.base.BindUpdateBy;
import com.common.base.DictionaryAware;
import com.common.entity.user.UserEntity;
import com.common.global.BizException;
import com.common.global.BizServiceEnum;
import com.common.requestvo.judicialcheck.JudicialCheckFillCountReqVo;
import com.common.requestvo.judicialcheck.JudicialCheckReqVo;
import com.common.service.judicialcheck.JudicialCheckService;
import com.common.service.system.UserService;
import com.common.utils.JwtTokenUtils;
import com.common.utils.PageUtil;
import com.common.utils.ResultVoUtil;
import com.common.utils.TokenUtils;
import com.common.vo.PageVo;
import com.common.vo.ResultVo;
import com.common.vo.judicialcheck.JudicialCheckVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 指标导入Controller
 *
 * @author johnny
 * @create 2021-05-15 8:17 上午
 **/

@Api(tags = "司法业绩考核")
@RestController
@RequestMapping("/judicial_check")
@Slf4j
public class JudicialCheckController {


    private final JudicialCheckService judicialCheckService;


    private final UserService userService;

    public JudicialCheckController(JudicialCheckService judicialCheckService, UserService userService) {
        this.judicialCheckService = judicialCheckService;
        this.userService = userService;
    }

    @ModelAttribute
    public JudicialCheckReqVo createByBind(JudicialCheckReqVo judicialCheckReqVo, HttpServletRequest request) {
        judicialCheckReqVo.setCreateBy(JwtTokenUtils.getUserNameFromReq(request));
        return judicialCheckReqVo;
    }

    @GetMapping("/listByCondition")
    @ApiOperation(value = "根据条件查询填表项信息 (分页)")
    public ResultVo listByCondition(JudicialCheckReqVo judicialCheckReqVo,
                                    PageVo pageVo,
                                    HttpServletRequest request) {
        pageVo.setPageNumber(pageVo.getPageNumber() - 1);

        List<String> userRolesFromToken = JwtTokenUtils.getUserRolesFromToken(TokenUtils.getTokenFromRequest(request));
        //判断是否 有查询全部的权限
        if (CollectionUtils.isEmpty(CollectionUtils.intersection(DictionaryAware.ALL_FILL_FORM_ROLES, userRolesFromToken))) {
            Long userId = JwtTokenUtils.getUserIdFromToken(TokenUtils.getTokenFromRequest(request));
            Long personalId = userService.findPersonalIdByUserId(userId);
            if (personalId == null) {
                throw new BizException(BizServiceEnum.FIND_NOT_CURRENT_USER_PERSONAL_INFO);
            }
            //如果没有 特定权限 则只能查看当前登录人的
            judicialCheckReqVo.setPersonalId(personalId);
        }
        Page<JudicialCheckVo> page = judicialCheckService.listByCondition(judicialCheckReqVo, PageUtil.initPage(pageVo));
        return ResultVoUtil.success(page);
    }


    @PostMapping("/fillJudicialCheckCount")
    @ApiOperation(value = "填表")
    @BindUpdateBy
    public ResultVo fillJudicialCheckCount(@RequestBody JudicialCheckFillCountReqVo judicialCheckFillCountReqVo) {
        judicialCheckService.fillJudicialCheckCount(judicialCheckFillCountReqVo);
        return ResultVoUtil.success();
    }




}